package com.lxp.uCenterService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxp.servicebase.exception.MyException;
import com.lxp.uCenterService.domain.UCenterMember;
import com.lxp.uCenterService.mapper.UcenterMemberMapper;
import com.lxp.uCenterService.service.UCenterMemberService;
import com.lxp.uCenterService.vo.RegisterVo;
import com.lxp.utils.JwtUtils;
import com.lxp.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author lxp
 * @since 2021-12-07
 */
@Service
public class UCenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UCenterMember> implements UCenterMemberService {

    @Autowired
    private RedisTemplate<String,String> template;

    //登录功能
    @Override
    public String login(UCenterMember member) {
        //获取登录的邮箱以及密码
        String email = member.getEmail();
        String password = member.getPassword();
        //判断输入是否为空
        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(password)){
            throw new MyException(20001,"登录出错");
        }

        //根据该邮箱获取到用户
        UCenterMember user = baseMapper.selectOne(
                new QueryWrapper<UCenterMember>()
                        .eq("email", email));
        if (user==null){//邮箱出错
            throw new MyException(20001,"邮箱或密码错误!!!");
        }
        //判断输入的密码和用户的密码是否对应
        //将输入的密码加密然后和用户的密码进行对比
        if (!MD5.encrypt(password).equals(user.getPassword())){
            //密码不对
            throw new MyException(20001,"邮箱或密码错误!!!");
        }

        //登录成功,将用户信息存入token
        return JwtUtils.getJwtToken(user.getId(), user.getNickname());
    }

    //注册方法
    @Override
    public void register(RegisterVo registerVo) {
        //获取registerVo里面的信息
        String code = registerVo.getCode();
        String password = registerVo.getPassword();
        String email = registerVo.getEmail();
        String nickname = registerVo.getNickname();

        //判断信息输入是否完整
        if (StringUtils.isEmpty(code)
                || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(email)
                || StringUtils.isEmpty(nickname) ){
            throw new MyException(20001,"信息输入不完整,请重新输入");
        }

        //判断验证码是否正确
        //取出redis存的验证码
        String redisCode = template.opsForValue().get(email);
        if(StringUtils.isEmpty(redisCode)){
            throw new MyException(20001,"验证码错误");
        }
        if (!redisCode.equals(code)){
            throw new MyException(20001,"验证码错误");
        }

        //判断邮箱是否被注册过了
        int temp = baseMapper.selectCount(new QueryWrapper<UCenterMember>().eq("email", email));
        if (temp>0){
            throw new MyException(20001,"邮箱已被注册");
        }
        //判断用户名是否被注册过
        temp = baseMapper.selectCount(new QueryWrapper<UCenterMember>().eq("nickname", nickname));
        if (temp>0){
            throw new MyException(20001,"用户名已被注册");
        }

        //注册成功写入数据库
        UCenterMember member=new UCenterMember();
        member.setEmail(email);
        member.setPassword(MD5.encrypt(password));
        member.setNickname(nickname);
        baseMapper.insert(member);
    }


    //根据token查询用户信息
    @Override
    public UCenterMember getUserLogin(HttpServletRequest request) {
        //调用工具类的方法获取token中的id值
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        UCenterMember userLogin = baseMapper.selectById(userId);
        if (userLogin==null){
            throw new MyException(20001,"登录出错");
        }
        return userLogin;
    }
}
